package MD.NJavaEyu;
import android.app.Activity;
import android.util.Log;

import com.eyu.common.ad.EyuAdManager;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import MD.NJavaBase.AdType;
import MD.NJavaBase.IAd;
import MD.NJavaBase.IAdCallback;
import MD.NJavaBase.NJavaBase;

public class EyuVideo implements IAd {
    static String Tag = "EyuVideo";

    public EyuVideo(int priority)
    {
        Log.d(Tag,"init");

        m_Priority = priority;
        Activity activity = NJavaBase.getActivity();

        m_CacheOk = false;
        m_Loading = false;
        m_Priority = priority;
        gEyuVideoInst = this;
        //等待一会儿才允许装载视频
        {
            Timer reloadTimer = new Timer();// 实例化Timer类
            class overTimeTask extends TimerTask {
                public void run() {
                    class reloadRunnable implements Runnable {
                        @Override
                        public void run() {
                            m_Initdone = true;
                        }
                    }
                    NJavaBase.getActivity().runOnUiThread(new reloadRunnable());
                    this.cancel();
                }
            }
            reloadTimer.schedule(new overTimeTask(), 5000);// 毫秒
        }
    }

    public AdType getAdType(){
        return AdType.rewardVideo;
    }

    public static EyuVideo I()
    {
        return gEyuVideoInst;
    }

    //是否已经初始化完毕，可以开始加载视频
    public boolean isInitialized() {return m_Initdone;}

    //广告平台的名称
    public String adName() { return "EyuVideo"; }

    //当前视频是否已经过期
    public boolean isInvalidated() { return !m_CacheOk; }

    //预装视频 回调0表示预装成功 99表示预装失败  2视频播放结束可以获得奖励 3视频已关闭
    public void reloadAd(IAdCallback callBack)
    {
        m_Callback = callBack;

        if( isInvalidated())//尚未装载成功
        {
            //立即发起装载
            _load();
        }
        else//已经是装载成功的
            callBack.onLoad();
    }

    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    //是否需要由loader去检查加载超时
    public boolean needCheckLoadOvertime() { return false; }

    //显示视频
    public void show() {
        if (!isInvalidated())//调用show之前，外部已经判断过是否装成功，如果这里返回false，应该是有问题
        {
            Log.d(Tag,"show#1");
            m_CacheOk = false;

            //reward_ad为广告位id，对应ios_ad_setting.json配置
            Activity activity = NJavaBase.getActivity();
            EyuAdManager.getInstance().showRewardedVideoAd(activity, "reward_ad");
        } else {
            Log.d(Tag,"show#2");
            _load();//立即发起装载
        }
    }

    void _load(){
        Log.d(Tag,"_load#1");
        if(m_Loading) return;
        m_Loading = true;

        if( m_CacheOk )//已经是装载成功的
        {
            Log.d(Tag,"_load#2");
            m_Loading = false;
            m_Callback.onLoad();
        }
    }

    //优先级，值越大越优先
    public  int getPriority() { return m_Priority; }


    ////////////////////////////////////////////////////
    //视频加载回调接口
    ////////////////////////////////////////////////////
    //广告装载成功
    public void onFinishLoadingAD() {

        m_CacheOk = true;

        if(m_Loading)//当前正在加载
        {
            Log.d(Tag,"onFinishLoadingAD#1");
            m_Loading = false;
            m_Callback.onLoad();
        }else
            Log.d(Tag,"onFinishLoadingAD#2");
    }

    //广告装载失败 一直等待底层sdk加载成功就好了
    public  void onFailToLoad() {
        //[m_Callback Recall:[NSNumber numberWithInt:99] evtName:@""];
    }

    //获得奖励
    void onRewarded(){
        Log.d(Tag,"onRewarded");
        m_Callback.onReward();
    }

    //视频关闭
    void onRewardedVideoClose() {
        Log.d(Tag,"onRewardedVideoClose");
        //通知关闭
        m_Callback.onClose();
    }




    int m_Priority;
    boolean m_Loading = false;
    boolean m_CacheOk = false;
    IAdCallback m_Callback;
    boolean m_Initdone = false;
    static EyuVideo gEyuVideoInst;
}